var t = require("/utils/util.js");

App({
    onLaunch: function(t) {
        1044 == t.scene && wx.getShareInfo({
            shareTicket: t.shareTicket,
            success: function(t) {
                t.encryptedData, t.iv;
            }
        }), this.login(), wx.getSystemInfo({
            success: function(t) {
                var e = 750 / t.windowWidth;
                wx.setStorageSync("kScreenW", e);
            }
        });
    },
    toLogin: function() {
        var t = getCurrentPages();
        "pages/start/start" != t[t.length - 1].route && wx.redirectTo({
            url: "/pages/start/start"
        });
    },
    checkToken: function() {
        var t = this, e = t.globalData.token;
        wx.request({
            url: t.globalData.url + "user/NomalUser/checkToken",
            data: {
                token: e
            },
            success: function(e) {
                if(e.data.responseCode==500) {
                (t.globalData.token = null, t.login());
                } 
            }
        });
    },
    login: function() {
        var t = this;
        t.globalData.token ? t.checkToken() : wx.login({
            success: function(e) {
                e.code ? wx.request({
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    method: "POST",
                    url: t.globalData.url + "user/NomalUser/NomalUserLogin",
                    data: {
                        code: e.code
                    },
                    success: function(e) {
                        console.log(e)
                        200 == e.data.responseCode ?(t.globalData.userId = e.data.data.user.userId, 
                        t.globalData.token = e.data.data.token, t.globalData.userInfo = e.data.data.user) : t.toLogin();
                        // wx.switchTab({
                        //     url: "/pages/home/home"
                        // });
                    }
                }) : console.log("获取用户登录态失败！" + e.errMsg);
            }
        });
    },
    getUserIsRegister: function() {
        var e = this;
        wx.login({
            success: function(a) {
                t.request(e, "user/NomalUser/NomalUserhasRegister", "POST", {
                    code: a.code
                }, function(t) {
                    t.state && (null != t.data ? wx.setStorageSync("UserRegister", !0) : (wx.setStorageSync("UserRegister", !1), 
                    e.toLogin()));
                }, function(t){});
            }
        });
    },
    globalData: {
        userInfo: null,
        url: "http://localhost:9091/",
        latitude: "",
        longitude: "",
        address: "",
        userId: ""
    }
});